﻿Imports DevComponents.DotNetBar.SuperGrid
Public Class DPRICESINGLE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DEFAULTPRICESINGLE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objSelectMasproductWithPriceDate As New masPrice

        Dim maspriceData As DataTable
        maspriceData = connect.GetTable(objSelectMasproductWithPriceDate.selectMasproductWithPriceDate_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        maspriceGrid.PrimaryGrid.DataSource = maspriceData

        DesignForm(Me)
        DesignGrid(maspriceGrid)
        DesignGrid(maspriceHistoryGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub maspriceGrid_CellClick(sender As Object, e As GridCellClickEventArgs) Handles maspriceGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        Try
            PRCIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสราคา").Value()
        Catch ex As Exception
            PRCIDTxt.Text = ""
        End Try

        PRDCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value()
        PRDNAMELabel.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผลิตภัณฑ์").Value()

        Try
            OPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("OPD").Value()
        Catch ex As Exception
            OPD_PRICETxt.Text = ""
        End Try

        Try
            IPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("IPD").Value()
        Catch ex As Exception
            IPD_PRICETxt.Text = ""
        End Try

        Try
            STDATEDate.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่เริ่มต้นใช้ราคา").Value()
        Catch ex As Exception
            STDATEDate.Text = ""
        End Try

        selectMaspriceHistory(PRDCODETxt.Text)
    End Sub

    Public Sub selectMaspriceHistory(ByVal prdCode As Integer)
        Dim queryString As String
        queryString = "SELECT `prcid` AS 'รหัสราคา', `prdcode` AS 'รหัสผลิตภัณฑ์', `st` AS 'วันเริ่มต้น', `en` AS 'วันสิ้นสุด', `opd_price` AS 'OPD', `ipd_price` AS 'IPD', `status` AS 'สถานะการเปิดใช้งาน' FROM masprice WHERE prdcode = '" & prdCode & "' ORDER BY st DESC"

        Dim maspriceHistoryData As DataTable
        maspriceHistoryData = connect.GetTable(queryString)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        maspriceHistoryGrid.PrimaryGrid.DataSource = maspriceHistoryData

        Me.Refresh()

        For i As Integer = 0 To maspriceHistoryData.Rows.Count - 1
            Dim dateStart As Date
            Dim dateEnd As Date
            dateStart = maspriceHistoryData.Rows(i)("วันเริ่มต้น")
            dateEnd = maspriceHistoryData.Rows(i)("วันสิ้นสุด")

            Dim result1 As Integer = DateTime.Compare(dateStart, Now)
            Dim result2 As Integer = DateTime.Compare(dateEnd, Now)

            'Dim result1 As Integer = DateDiff(DateInterval.Day, dateStart, Date.Now)
            'Dim result2 As Integer = DateDiff(DateInterval.Day, dateEnd, Date.Now)

            'MsgBox(result1 & "/" & result2)

            If result1 < 0 And result2 < 0 Then
                'MsgBox("past")
                CType(maspriceHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(179, 179, 179)
            ElseIf result1 <= 0 And result2 >= 0 Then
                'MsgBox("present")
                CType(maspriceHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(171, 217, 93)
            ElseIf result1 > 0 And result2 > 0 Then
                'MsgBox("future")
                CType(maspriceHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(122, 156, 255)
            End If
        Next
    End Sub

    Private Sub maspriceHistoryGrid_CellClick(sender As Object, e As GridCellClickEventArgs) Handles maspriceHistoryGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        historyPRCIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสราคา").Value()

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("masprice", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "PRCID", historyPRCIDTxt.Text)
    End Sub

    Private Sub importMasprice_Click(sender As Object, e As EventArgs)
        Dim nextform As New IMPORTEXCEL(masprice.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub addMaspriceBtn_Click_1(sender As Object, e As EventArgs) Handles addMaspriceBtn.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If PRDCODETxt.Text.ToString.Length > 0 And OPD_PRICETxt.Text.ToString.Length > 0 And IPD_PRICETxt.Text.ToString.Length > 0 And STDATEDate.Text.ToString.Length > 0 Then

                    Dim result As Integer = DateTime.Compare(STDATEDate.Value.Date, DateTime.Now.Date)
                    If result >= 0 Then
                        Dim objInsertData As New masPrice
                        If PRCIDTxt.Text.ToString.Length > 0 Then
                            objInsertData.PRCID_ = PRCIDTxt.Text
                        End If

                        objInsertData.PRDCODE_ = PRDCODETxt.Text

                        'Dim startDay As String = STDATEDate.Value.Day
                        'Dim startMonth As String = STDATEDate.Value.Month
                        'Dim startYear As String = STDATEDate.Value.Year
                        'objInsertData.ST_ = startYear & "-" & startMonth & "-" & startDay
                        objInsertData.ST_ = STDATEDate.Value.Date

                        objInsertData.OPD_PRICE_ = OPD_PRICETxt.Text
                        objInsertData.IPD_PRICE_ = IPD_PRICETxt.Text
                        objInsertData.STATUS_ = maspriceStatusChk.Checked

                        objInsertData.insertMasprice()

                        Dim objSelectMasproductWithPriceDate As New masPrice

                        Dim maspriceData As DataTable
                        maspriceData = connect.GetTable(objSelectMasproductWithPriceDate.selectMasproductWithPriceDate_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        maspriceGrid.PrimaryGrid.DataSource = maspriceData

                        selectMaspriceHistory(PRDCODETxt.Text)
                    Else
                        MsgBox("ไม่สามารถระบุวันที่ในอดีตได้")
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class